<script>
	var util = require('./utils/util.js');
	var api = require('./config/api.js');
	// var user = require('./services/user.js');
	import push from '@/common/dc-push/push.js';
export default {
	onLaunch: function() {
	
		console.log('App Launch');
		
		// #ifdef APP-PLUS
		let _this = this;
		var info = plus.push.getClientInfo();
		console.log(info);
		// push.on();
		// setTimeout(() => {
		// 	//console.log('push' + push.on()); 
		//push.on()返回false代表没开推送，这里可以做个友好提示弹出提示用户是否开启推送，确认开启就调用 push.on()跳转到设置界面
		if (!push.isOn()) {
			
			
			// push.on();
			
			console.log('没有开启推送')
			
			
		}
		//  }, 1000);
		const _handlePush = function(message) {
			/**
			 * 实际开发中，这里可能是跳转到某个页面等操作，请根据自身业务需求编写。
			 */
			//console.log('message' + JSON.stringify(message));
			if (typeof message.payload == 'string') {
				message.payload = JSON.parse(message.payload);
			}
			
			if (message.payload && message.payload.activity) {
				console.log('message',message);
				// var data = new Object();
				// data.messageId= message.payload.param.messageId;
				// // //console.log('formData',formData);
				// util.request(api.messageRead, data,'POST', '','formatDate' ).then((res) => {
				// 	//console.log('消息中心',JSON.stringify(res));
		
				// });
			
		
				if (message.payload.activity == '/pages/orderDetails/orderDetails') {
					// 跳转webview页面  把url塞入缓存
					
					if(uni.getStorageSync('identity')==3){
						uni.navigateTo({
							url: message.payload.activity + "?orderId=" + message.payload.param.orderId + '&istuisong=1&isDetail=undefined'
						});
					}else{
						uni.navigateTo({
							url: message.payload.activity + "?orderId=" + message.payload.param.orderId + '&istuisong=1'
						});
					}
				
					
				}else{
					uni.navigateTo({
						url:"/pages/message/message"
					})
				}
			}
			// http://localhost:8084/#/packageA/pages/goods/goods?id=207
		

/* 
				uni.showModal({
					
					title: '推送',
					content: JSON.stringify(message),
					showCancel: false,
					cancelText: '',
					confirmText: '确定',
					success: res => {},
					fail: () => {},
					complete: () => {}
				}); */
		};
		
		
		plus.push.getClientInfoAsync(
			info => {
				// //console.log(JSON.stringify(info.clientid));
					 
				console.log('执行函数获取token');
				uni.setStorageSync('cid', info.clientid);
				console.log('cid',info.clientid);
		
				setTimeout(() => {
					if (uni.getStorageSync('token')) {
						_this.uploadDeviceToken();
					}
				}, 2000)
				//上传deviceTokencom.jiuetao.merchant
			},
			e => {
				
			}
		);
		plus.push.addEventListener(
			'click',
			function(message) {
				//判断有没有token  有token直接条  没有直接跳登陆页
				var token = uni.getStorageSync('token');
				if (token) {
					_handlePush(message);
				} else {
					uni.redirectTo({
						url: "/pages/login/login"
					});
				}
				// plus.nativeUI.toast('push click');
		
			},
			false
		);
		// 这里注释   只有在点击的情况下触发 
		// IOS 才收到这个提醒
		let sysInfo = uni.getSystemInfoSync();
		if(sysInfo.platform=='ios'){
			plus.push.addEventListener(
				'receive',
				function(message) {
					uni.showToast({
						icon:'none',
						title: "你有新的订单推送请前往查看!",
						duration: 2000,
					})
					// _handlePush(message);
				},
				false
			);
		}
		
		// #endif
	},
	onShow: function() {
		console.log('App Show');
		
	},
	onHide: function() {
		console.log('App Hide');
	},
	methods: {
		uploadDeviceToken() {
			util.request(
			api.deviceToken, {
				deviceToken: uni.getStorageSync('cid')
			}, 'POST');
		}
	}
};
</script>

<style>
	.public-line-height{
		height:1px;
		background:rgba(245,245,245,1);
	}
/* 解决头条小程序组件内引入字体不生效的问题 */
/* #ifdef MP-TOUTIAO */
@font-face {
	font-family: uniicons;
	src: url('/static/uni.ttf');
}
/* #endif */
</style>
